# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'DB_initialform.ui'
##
## Created by: Qt User Interface Compiler version 6.5.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QFormLayout, QLabel, QLineEdit,
    QPushButton, QSizePolicy, QVBoxLayout, QWidget)

class Ui_DBinitialForm(object):
    def setupUi(self, DBinitialForm):
        if not DBinitialForm.objectName():
            DBinitialForm.setObjectName(u"DBinitialForm")
        DBinitialForm.resize(336, 227)
        self.formLayoutWidget = QWidget(DBinitialForm)
        self.formLayoutWidget.setObjectName(u"formLayoutWidget")
        self.formLayoutWidget.setGeometry(QRect(40, 30, 251, 91))
        self.formLayout = QFormLayout(self.formLayoutWidget)
        self.formLayout.setObjectName(u"formLayout")
        self.formLayout.setContentsMargins(0, 0, 0, 0)
        self.DB_port_label = QLabel(self.formLayoutWidget)
        self.DB_port_label.setObjectName(u"DB_port_label")
        font = QFont()
        font.setPointSize(9)
        self.DB_port_label.setFont(font)

        self.formLayout.setWidget(1, QFormLayout.LabelRole, self.DB_port_label)

        self.DBport_Edit = QLineEdit(self.formLayoutWidget)
        self.DBport_Edit.setObjectName(u"DBport_Edit")
        self.DBport_Edit.setFont(font)
        self.DBport_Edit.setMaxLength(4)

        self.formLayout.setWidget(1, QFormLayout.FieldRole, self.DBport_Edit)

        self.DB_username_label = QLabel(self.formLayoutWidget)
        self.DB_username_label.setObjectName(u"DB_username_label")
        self.DB_username_label.setFont(font)
        self.DB_username_label.setLayoutDirection(Qt.LeftToRight)

        self.formLayout.setWidget(2, QFormLayout.LabelRole, self.DB_username_label)

        self.DBusername_Edit = QLineEdit(self.formLayoutWidget)
        self.DBusername_Edit.setObjectName(u"DBusername_Edit")
        self.DBusername_Edit.setFont(font)

        self.formLayout.setWidget(2, QFormLayout.FieldRole, self.DBusername_Edit)

        self.DB_userpw_label = QLabel(self.formLayoutWidget)
        self.DB_userpw_label.setObjectName(u"DB_userpw_label")
        self.DB_userpw_label.setFont(font)

        self.formLayout.setWidget(3, QFormLayout.LabelRole, self.DB_userpw_label)

        self.DBuserpw_Edit = QLineEdit(self.formLayoutWidget)
        self.DBuserpw_Edit.setObjectName(u"DBuserpw_Edit")
        self.DBuserpw_Edit.setFont(font)
        self.DBuserpw_Edit.setEchoMode(QLineEdit.Password)

        self.formLayout.setWidget(3, QFormLayout.FieldRole, self.DBuserpw_Edit)

        self.layoutWidget = QWidget(DBinitialForm)
        self.layoutWidget.setObjectName(u"layoutWidget")
        self.layoutWidget.setGeometry(QRect(90, 140, 145, 61))
        self.verticalLayout = QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.DBInitial_Button = QPushButton(self.layoutWidget)
        self.DBInitial_Button.setObjectName(u"DBInitial_Button")
        self.DBInitial_Button.setFont(font)
        self.DBInitial_Button.clicked.connect(self.write_dbconfigFile) #单独加入的代码

        self.verticalLayout.addWidget(self.DBInitial_Button)

        self.statusshow_label = QLabel(self.layoutWidget)
        self.statusshow_label.setObjectName(u"statusshow_label")
        self.statusshow_label.setFont(font)

        self.verticalLayout.addWidget(self.statusshow_label)


        self.retranslateUi(DBinitialForm)

        QMetaObject.connectSlotsByName(DBinitialForm)
    # setupUi

    def retranslateUi(self, DBinitialForm):
        DBinitialForm.setWindowTitle(QCoreApplication.translate("DBinitialForm", u"\u6570\u636e\u5e93\u8fde\u63a5\u4e0e\u521d\u59cb\u5316", None))
        self.DB_port_label.setText(QCoreApplication.translate("DBinitialForm", u"\u6570\u636e\u5e93\u7aef\u53e3\u53f7\uff1a", None))
#if QT_CONFIG(tooltip)
        self.DBport_Edit.setToolTip(QCoreApplication.translate("DBinitialForm", u"\u9ed8\u8ba4\u4e3a\uff1a5432", None))
#endif // QT_CONFIG(tooltip)
#if QT_CONFIG(statustip)
        self.DBport_Edit.setStatusTip("")
#endif // QT_CONFIG(statustip)
#if QT_CONFIG(whatsthis)
        self.DBport_Edit.setWhatsThis("")
#endif // QT_CONFIG(whatsthis)
        self.DBport_Edit.setInputMask(QCoreApplication.translate("DBinitialForm", u"9999", None))
        self.DBport_Edit.setText(QCoreApplication.translate("DBinitialForm", u"5432", None))
        self.DB_username_label.setText(QCoreApplication.translate("DBinitialForm", u"\u7528\u6237\u540d\uff1a", None))
#if QT_CONFIG(tooltip)
        self.DBusername_Edit.setToolTip(QCoreApplication.translate("DBinitialForm", u"\u9ed8\u8ba4\u4e3a\uff1apostgres", None))
#endif // QT_CONFIG(tooltip)
        self.DBusername_Edit.setInputMask(QCoreApplication.translate("DBinitialForm", u"NNNNNNNN", None))
        self.DBusername_Edit.setText(QCoreApplication.translate("DBinitialForm", u"postgres", None))
        self.DB_userpw_label.setText(QCoreApplication.translate("DBinitialForm", u"\u7528\u6237\u767b\u5f55\u53e3\u4ee4\uff1a", None))
        self.DBInitial_Button.setText(QCoreApplication.translate("DBinitialForm", u"\u8fde\u63a5\u5e76\u521d\u59cb\u5316\u6570\u636e\u5e93", None))
        self.statusshow_label.setText("")
    # retranslateUi

    def write_dbconfigFile(self):
        import configparser
        import os
        import psycopg2
        
        try:  
            conn = psycopg2.connect(
                host="localhost",
                port=self.DBport_Edit.text(),
                database="postgres",#也可用dbname="postgres"？
                user=self.DBusername_Edit.text(),
                password=self.DBuserpw_Edit.text(),
            )
            config = configparser.ConfigParser()
            config.read('.\dbinitial\dbconfig.ini') 
            config['Database Connect'] = {}
            config['Database Connect']['host'] = "http://localhost"
            config['Database Connect']['port'] = self.DBport_Edit.text()
            config['Database Connect']['database'] = "postgres"
            config['Database Connect']['username'] = self.DBusername_Edit.text()   
            config['Database Connect']['userpw'] = self.DBuserpw_Edit.text()

            config_file_path = os.path.join(os.getcwd(), '.\dbinitial\dbconfig.ini')
            with open(config_file_path, 'w') as config_file:
                config.write(config_file)    
            
            self.statusshow_label.setText("连接成功，请关闭窗口！") 
            
            self.DBInitial_Button.setEnabled(False)
            
        except (Exception, psycopg2.DatabaseError) as error:
            self.statusshow_label.setText("连接失败，请重新输入！")     


    

